package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import model.DrawableListModel;
import model.IDrawable;
import model.Point2D;

public class DrawPanel extends JPanel {

	private DrawableListModel theListModel;

	public DrawPanel(DrawableListModel listModel) {
		super();
		theListModel = listModel;
		this.addMouseListener(new Point2DMouseAdapter());
	}
	
	@Override
	protected void paintComponent(Graphics arg0) {
		super.paintComponent(arg0);
		arg0.setColor(Color.green);
		//TODO zeichne alle Drawables
		for(int i = 0; i < theListModel.getSize(); i++) {
			IDrawable drawable = (IDrawable) theListModel.getElementAt(i);
			drawable.paint((Graphics2D) arg0);
		}
	}
	
	/**
	 * setzten eines Punktes in der 2D Grafik per Mausklick
	 * @author Marcus Alt
	 *
	 */
	
	private class Point2DMouseAdapter extends MouseAdapter{

		@Override
		public void mouseClicked(MouseEvent e) {
			int x = e.getX();
			int y = e.getY();
			theListModel.addElement(new Point2D(x, y));
			DrawPanel.this.repaint();
		}
		
	}
	
	

}
